package com.zsrt.day20170414.pm.daoimpl;

import  com.zsrt.day20170414.pm.entity.StudentEntity;
import com.zsrt.utils.ArrayUtil;
import com.zsrt.day20170414.pm.dao.StudentDao;

/**
*@author jzh
*@创建时间 2017年4月14日下午6:18:03
*类说明：
*/
public class StudentDaoImpl implements StudentDao {

	@Override
	public int studentExistOrNotByName(StudentEntity[] students, String name) {
		int xb=-1;
		//ArrayUtil.printArray(students);
		for(int index=0;index<students.length;index++){
			StudentEntity student = students[index];
			
			String newName=student.getName();
			if(newName.equals(name)){
				return index;
			}
		}
		return xb;
	}

	@Override
	public StudentEntity[] addStudent(StudentEntity[] students, StudentEntity student) {
		StudentEntity[] newStudents=new StudentEntity[students.length+1];
		String name = student.getName();
		int index=studentExistOrNotByName(students, name);
		if(index>=0){
			System.out.println("该学生已存在，请重新输入");
		}else{
			for(int i=0;i<students.length;i++){
				newStudents[i]=students[i];
			}
			newStudents[students.length]=student;
		}
		System.out.println("..........");
		return newStudents;
	}

	@Override
	public StudentEntity[] deleteStudentByName(StudentEntity[] students, String name) {
		//newStudents用来保存删除后的数组
		StudentEntity[] newStudents=new StudentEntity[students.length-1];
		int index=studentExistOrNotByName(students, name);
		if(index>=0){
			for(int i=0;i<index;i++){
				newStudents[i]=students[i];
			}
			for(int i=index+1;i<students.length;i++){
				newStudents[i-1]=students[i];				
			}
		}
		return newStudents;	
	}

	@Override
	public StudentEntity[] deleteStudentByNames(StudentEntity[] students, String[] delStudents) {
		//System.out.println("输入要删除的学生数量");
		int count=delStudents.length;
		StudentEntity[][] newStudents=new StudentEntity[count+1][];
		for(int index=0;index<count;index++){
			newStudents[count-index-1]=new StudentEntity[students.length-index-1];
		}
		newStudents[count]=students;
		for(int index=0;index<count;index++){
			newStudents[count-index-1]=deleteStudentByName(newStudents[count-index],delStudents[index]);
		}
		
		return newStudents[0];
	}

	@Override
	public StudentEntity updateStudentScore(StudentEntity[] students, String name, double score) {
		int index=studentExistOrNotByName(students, name);
		if(index>=0){
			students[index].setScore(score);
		}
		return students[index];
	}

	/*@Override
	public StudentEntity findStudentByName(StudentEntity[] students, String name) {
		int index=studentExistOrNotByName(students, name);
		if(index<0){
			System.out.println("该学生不存在");
		}else{
			
		}
		return null;
	}*/

	@Override
	public StudentEntity[] findStudentByAge(StudentEntity[] students, int age) {
		int counter=0;
		for(int index=0;index<students.length;index++){
			StudentEntity student=new StudentEntity();
			student=students[index];
			int age1=student.getAge();
			if(age1>age){
				counter++;
			}
		}
		StudentEntity[] newStudents=new StudentEntity[counter];
		counter=0;
		for(int index=0;index<students.length;index++){
			StudentEntity student=new StudentEntity();
			student=students[index];
			double score=student.getScore();
			if(score<60){
				
				newStudents[counter]=student;
				counter++;
			}
		}
		return newStudents;
	}
	
	public int login(String phone,String password){
		if(phone.equals("13926583118")){
			if(password.equals("123456")){
				//System.out.println("登录成功");
				return 1;
			}else{
				//System.out.println("密码错误，请重新输入");
				return 0;	
			}
		}else{
			return -1;
		}
	}

}
